package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Company;
import com.itheima.service.store.CompanyService;
import com.itheima.service.store.impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/company")
public class CompanyServlet extends HttpServlet {

    CompanyService companyService=new CompanyServiceImpl();

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码    在filter 过滤器中写
//        req.setCharacterEncoding("UTF-8");
//        resp.setContentType("text/html;charset=UTF-8");

        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            findAll(req, resp);
        } else if ("toAdd".equals(operation)) {
            req.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(req,resp);
        } else if ("save".equals(operation)) {
         //   Map<String, String[]> parameterMap = req.getParameterMap();
            Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
            companyService.save(company);
            resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
        } else if ("delete".equals(operation)) {
            String id = req.getParameter("id");
            companyService.delete(id);
            resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
        } else if ("toEdit".equals(operation)) {
            String id = req.getParameter("id");
            Company company = companyService.findById(id);
            req.setAttribute("company",company);
            req.getRequestDispatcher(req.getContextPath()+"/WEB-INF/pages/store/company/update.jsp").forward(req,resp);
        } else if ("edit".equals(operation)) {
            Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
            companyService.update(company);
            resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
        }


    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page=1;
        int size=5;
        String page1 = req.getParameter("page");
        String size1 = req.getParameter("size");
        if (StringUtils.isNotBlank(page1)) {
            page = Integer.parseInt(page1);
        }

        if (StringUtils.isNotBlank(size1)) {
            size=Integer.parseInt(size1);
        }
        List<Company> all = companyService.findAll(page, size);
        PageInfo<Company> companyPageInfo = new PageInfo<>(all);
        req.setAttribute("page",companyPageInfo);
        //转发 web-inf 前要加"/"
        req.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(req,resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
